<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-08-07
 * Time: 15:08
 */

namespace App\Traits;

use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

trait FilterParameters
{
    /**
     * 处理筛选参数
     * @param array $data
     * @return array
     */
    public function handleFilterUserCondition(array $data){

        /**
         * 处理筛选数据 TODO 模糊搜索
         * 使用包tucker-eric/eloquentfilter 已移除
         * https://packagist.org/packages/tucker-eric/eloquentfilter
         */
        $arrData = [];
        data_get($data, 'username') && array_push($arrData, ['username', 'like', $data['username'] . '%']);
        data_get($data, 'realname') && array_push($arrData, ['realname', 'like', $data['realname'] . '%']);
        data_get($data, 'mobile') && array_push($arrData, ['mobile', 'like', $data['mobile'] . '%']);
        data_get($data, 'gender') && array_push($arrData, ['gender', $data['gender']]);
        data_get($data, 'user_type') && array_push($arrData, ['user_type', $data['user_type']]);
        array_has($data, 'status') && array_push($arrData, ['status', $data['status']]);

        //TODO 时间处理
        data_get($data, 'start_time') && array_push($arrData, ['created_at', '>=', utc_date($data['start_time'])]);
        data_get($data, 'end_time') && array_push($arrData, ['created_at', '<=', utc_date($data['end_time'])]);

        return $arrData;
    }

    /**
     * @param array $all
     * @return array
     */
    public function handleFilterBannerCondition(array $all){

        $data = [];
        data_set($data, 'name', $all['name']);
        data_set($data, 'createBy',Auth::user()->nickname);
        data_set($data, 'updateBy', Auth::user()->nickname);
        data_set($data, 'sort', data_get($all, 'sort', 0));
        data_set($data, 'status', data_get($all, 'status', 0));
        data_set($data, 'sortNo', data_get($all, 'sortNo', 0));
        data_get($all, 'image') && data_set($data, 'image', $all['image']);
        data_set($data, 'themeImage', data_get($all, 'themeImage',0));
        data_set($data, 'type', data_get($all, 'type', 0));
        data_set($data, 'positionId', data_get($all, 'positionId', 1));
        data_set($data, 'linkId', data_get($all, 'linkId', 0));
        data_get($all, 'validity') && data_set($data, 'validity', $all['validity']);
        data_get($all, 'content') && data_set($data, 'content', $all['content']);
        data_set($data, 'createTime', Carbon::now());
        data_set($data, 'updateTime', Carbon::now());
        data_get($all, 'startTime') && data_set($data, 'startTime', $all['startTime']);
        data_get($all, 'endTime') && data_set($data, 'endTime', $all['endTime']);

        return $data;
    }
}
